#include "TankManager.hpp"
#include "PlayerTank.hpp"
#include "Logger.hpp"


TankManager::TankManager():
m_tanks()
{
}
TankManager::~TankManager()
{

}		


void TankManager::update()
{
	std::map<unsigned int,TankPtr>::iterator itor = m_tanks.begin();
	for(;itor != m_tanks.end(); ++itor)
		itor->second->update();
}

void TankManager::createPlayerTank(const unsigned int id)
{
	if(m_tanks.find(id) == m_tanks.end())
	{
		LogInfo << "Create player tank (id:" << int(id) << ")." << LogEnd; 
		TankPtr ptr(new PlayerTank());
		m_tanks[id] = ptr;
	}
	else
		LogWarn << "Tried to add twice the same tank (id:" << int(id) << ") !" << LogEnd;

}

void TankManager::createNetTank(const unsigned int id)
{

	if(m_tanks.find(id) == m_tanks.end())
	{
		LogInfo << "Create network tank (id:" << int(id) << ")." << LogEnd;
		// not implemented yet
		/*TankPtr tank = new PlayerTank();
		m_tanks[id] = tank;*/
	}
	else
		LogWarn << "Tried to add twice the same tank (id:" << int(id) << ") !" << LogEnd;
}
		
void TankManager::destroyTank(const unsigned int id)
{
	if(m_tanks.find(id) != m_tanks.end())
	{
		LogInfo << "Destroy tank (id:" << int(id) << ")." << LogEnd;
		m_tanks.erase(id);
	}	
	
	else
		LogWarn << "Tried to destroy an unknown tank (id: " << int(id) << ") !" << LogEnd;
}

void TankManager::clearTanks()
{
	m_tanks.clear();
}

std::list<TankPtr> TankManager::tanks()
{
	std::list<TankPtr> list;
	std::map<unsigned int,TankPtr>::iterator itor = m_tanks.begin();
	for(; itor != m_tanks.end(); ++itor)
		list.push_back(itor->second);

	return list;
}

TankPtr TankManager::tank(const unsigned int id)
{
	if(m_tanks.find(id) != m_tanks.end())
		return m_tanks[id];
		
	else 
		return TankPtr();
}
